#ifndef __USERTYPE_CPP__
#define __USERTYPE_CPP__

#include "env.h"
#include "bytearrays.h"
#include "section.h"
#include "output.h"
#include "msghandle.h"
#include "msgqueue.h"

unsigned char num2char(int n)
{
	switch (n) {
		case 0:return '0';break;
		case 1:return '1';break;
		case 2:return '2';break;
		case 3:return '3';break;
		case 4:return '4';break;
		case 5:return '5';break;
		case 6:return '6';break;
		case 7:return '7';break;
		case 8:return '8';break;
		case 9:return '9';break;
		case 10:return 'A';break;
		case 11:return 'B';break;
		case 12:return 'C';break;
		case 13:return 'D';break;
		case 14:return 'E';break;
		case 15:return 'F';break;
	}
	return ' ';
}

std::string hex2str(std::string hex)
{
	if (hex.length() < 1) return "";
	int len = hex[0];
	if (len > MAX_SIZE) return "";
	std::string str = "";
	for (int i = 0;i < len;i++) {
		unsigned char c = hex[i+1];
		str += num2char(c/16);str += num2char(c%16);
	}
	return str;
}

unsigned char char2num(unsigned char c)
{
	switch (c) {
		case '0':return 0;break;
		case '1':return 1;break;
		case '2':return 2;break;
		case '3':return 3;break;
		case '4':return 4;break;
		case '5':return 5;break;
		case '6':return 6;break;
		case '7':return 7;break;
		case '8':return 8;break;
		case '9':return 9;break;
		case 'A':return 10;break;
		case 'B':return 11;break;
		case 'C':return 12;break;
		case 'D':return 13;break;
		case 'E':return 14;break;
		case 'F':return 15;break;
	}
	return 0;
}

std::string str2hex(std::string str)
{
	int len = str.length()/2;
	std::string hex = "";
	hex += (unsigned char)len;
	for (int i = 0;i < len;i++) {
		hex += char2num(str[i*2])<<4+char2num(str[i*2+1]);
	}
	return hex;
}

#endif //__USERTYPE_CPP__
